package com.yuanzong.wuliu.service.app.business.customer;

import cn.hutool.core.collection.CollUtil;
import com.yuanzong.wuliu.common.exception.BusinessException;
import com.yuanzong.wuliu.common.vo.ApiResult;
import com.yuanzong.wuliu.controller.app.business.customer.param.AppBusinessCustomerDaijiaxianQueryByPhoneParam;
import com.yuanzong.wuliu.controller.intercdeptor.AppHandlerInterceptorAdapter;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomer;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerDaijiaxian;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerDaijiaxianDetails;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianDetailsListQuery;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerDaijiaxianListQuery;
import com.yuanzong.wuliu.service.business.customer.factory.BusinessCustomerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class AppBusinessCustomerDaijiaxianService {

    public ApiResult queryByPhone(AppBusinessCustomerDaijiaxianQueryByPhoneParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数验证
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomer businessCustomer = BusinessCustomerFactory.getBusinessCustomerService().queryByPhone(param.getPhone());
        if (businessCustomer == null) {
            throw new BusinessException("代驾险不存在");
        }
        BusinessCustomerDaijiaxianListQuery businessCustomerDaijiaxianListQuery = new BusinessCustomerDaijiaxianListQuery();
        businessCustomerDaijiaxianListQuery.setCustomerId(businessCustomer.getId());
        List<BusinessCustomerDaijiaxian> businessCustomerDaijiaxians = BusinessCustomerFactory.getBusinessCustomerDaijiaxianService().querySubject(businessCustomerDaijiaxianListQuery);
        if (CollUtil.isEmpty(businessCustomerDaijiaxians)) {
            throw new BusinessException("代驾险不存在");
        }
        BusinessCustomerDaijiaxian businessCustomerDaijiaxian = businessCustomerDaijiaxians.get(0);
        if (businessCustomerDaijiaxian.getEndDate() == null
                || businessCustomerDaijiaxian.getEndDate().isBefore(LocalDate.now())) {
            throw new BusinessException("您的保单已过期！为了您的权益，请尽快续保。微信：fangkuaiwuliu");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerDaijiaxianDetailsListQuery businessCustomerDaijiaxianDetailsListQuery = new BusinessCustomerDaijiaxianDetailsListQuery();
        businessCustomerDaijiaxianDetailsListQuery.setDaijiaxianId(businessCustomerDaijiaxian.getId());
        List<BusinessCustomerDaijiaxianDetails> daijiaxianDetailses = BusinessCustomerFactory.getBusinessCustomerDaijiaxianDetailsService().querySubject(businessCustomerDaijiaxianDetailsListQuery);
        businessCustomerDaijiaxian.setDetailses(daijiaxianDetailses);
        return ApiResult.success(businessCustomerDaijiaxian);
    }
}
